<script type="text/javascript">
var slash= "-";
var minYear=1900;
var maxYear=2100;

function isDateKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode == 45) {
        return true;
    }				
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

function isInteger(s){
    var i;
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function stripCharsInBag(s, bag){
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31;
        if (i==4 || i==6 || i==9 || i==11) {
            this[i] = 30;
        }
        if (i==2) {
            this[i] = 29;
        }
    } 
    return this
}

function pagingSubmit(){
	var currentPageNum = jQuery("#paging").find(".page-selector select").val();
	var pageSize = jQuery("#paging").find("#itemsPerPage select").val();			
	window.location.href = "${bean.pagingUrl}&currentPageNum=" + currentPageNum + "&pageSize=" + pageSize;			
}

function isDate(dtStr){
    var daysInMonth = DaysArray(12);
    
    var pos1=dtStr.indexOf(slash);
    var pos2=dtStr.indexOf(slash,pos1+1);
    
    var strYear=dtStr.substring(0,pos1);
    var strMonth=dtStr.substring(pos1+1,pos2);
    var strDay=dtStr.substring(pos2+1);
    
    strYr=strYear;
    
    if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
    if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
    
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
    }
    
    month=parseInt(strMonth);
    day=parseInt(strDay);
    year=parseInt(strYr);
    
    if (pos1==-1 || pos2==-1){
        return false;
    }
    if (strMonth.length<1 || month<1 || month>12){
        return false;
    }
    if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
        return false;
    }
    if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
        return false;
    }
    if (dtStr.indexOf(slash,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, slash))==false){
        return false;
    }
    return true
}

function searchActivityLogs(){
    var isValid = true;
    var msg = "<ul>";
    
    var from = jQuery.trim(jQuery("input[name='from']").val());
    if(from!=''){
        if(!isDate(from)){
            isValid = false;
            msg+='<li><spring:message code="activity.log.from.invalid"/></li>';
        }
    }
    
    var to = jQuery.trim(jQuery("input[name='to']").val());
    if(to!=''){
        if(!isDate(to)){
            isValid = false;
            msg+='<li><spring:message code="activity.log.to.invalid"/></li>';
        }
    }
    
    if(isValid){
        var df   =  parseInt(from.substring(8,10),10); 
        var mf   =  parseInt(from.substring(5,7),10);
        var yf   =  parseInt(from.substring(0,4),10); 		
        var _from = new Date(yf,mf-1,df);  
       
        var dt   =  parseInt(to.substring(8,10),10); 
        var mt   =  parseInt(to.substring(5,7),10);
        var yt   =  parseInt(to.substring(0,4),10); 		
        var _to = new Date(yt,mt-1,dt); 
       
        if(_from>_to){
            isValid = false;
            msg+='<li><spring:message code="activity.log.from.and.to.invalid"/></li>';
        }
    }
    
    if(isValid){
    	jQuery('input[name=cmd]').val('searchActivityLogs');
        submitForm('activityLogForm');
    }else{
       jQuery(".check-date span").html(msg); 
    }
}

function removeActivityLogs(){
    var count = jQuery('input.ckbChild:checked').length;
    if (count == 0){
        jQuery('#removeActivitiesDialog').html('<spring:message code="warning.activity.log.list.of.items.empty"/>');
        jQuery('#removeActivitiesDialog').dialog({
            modal:true,
            resizable: false,
            height: 120,
            width:470,	
            title:'<spring:message code="message"/>',
            buttons:{
            	'<spring:message code="button.cancel"/>' : function() {
                    jQuery("#removeActivitiesDialog").dialog("close");
                }
            }	
        });
    }else{
        jQuery('#removeActivitiesDialog').html('<spring:message code="warning.activity.log.remove.items"/>');
        jQuery('#removeActivitiesDialog').dialog({
            modal:true,
            resizable: false,
            height: 120,
            width:470,	
            title:'<spring:message code="message"/>',		         
            buttons: {
            	'<spring:message code="button.yes"/>' : function() { 
            		jQuery('input[name=cmd]').val('deleteActivityLogs');
                    submitForm('activityLogForm');  
                },
                '<spring:message code="button.no"/>' : function() {
                    jQuery('#removeActivitiesDialog').dialog("close");				  	       
                }
            }
        }); 	
    }	
}

jQuery(document).ready(function(){    
    $('input[title]').inputHints();	
    
    jQuery("input[name='from']").datepicker(pickerOpts);
    jQuery("input[name='to']").datepicker(pickerOpts);
        
    jQuery("input.ckbSelectAll").click(function(){
        if(this.checked == true) {
            jQuery("input.ckbChild").attr("checked",true);
        }
        else {
            jQuery("input.ckbChild").attr("checked",false);
        }
    });
		
    jQuery("input.ckbChild").click(function(){
        if(this.checked == false) {
            jQuery("input.ckbSelectAll").attr("checked", false);
        }
        var count = jQuery('input.ckbChild:checked').length;
        var max = jQuery('input[name="total"]').val();
        if(count == max){
            jQuery("input.ckbSelectAll").attr("checked", true);  
        }
    });
        
    jQuery("input[name='removeActivityLogs']").click(function(){
    	jQuery("#removeActivitiesDialog").css('height','15px');
        removeActivityLogs();
    });
    
    jQuery("input[name='searchActivityLogs']").click(function(){
        searchActivityLogs();     
    });
    
    var count = jQuery('input.ckbChild:checked').length;
    if(count == 0){
        jQuery("input.ckbSelectAll").attr("checked", false);  
    }
    
    jQuery('div#removeActivitiesDialog').css("height", "auto")

});
</script>